package com.stephenone.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

    private static final String TAG = MyService.class.getSimpleName();
    public int count;

    private MyBinder binder = new MyBinder();
    private boolean isQuited;


    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate: ");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!isQuited){
                    count ++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind: ");
        return binder;
    }


    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG, "onUnbind: " );
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isQuited = true;
        Log.e(TAG, "onDestroy: ");
    }

    public class MyBinder extends Binder {
        public int getCount(){
            return count;
        }
    }
}
